<template>
	<el-dialog :title="`表单字段`+titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<div v-for="item in fieldList" :key="item.prop">
				<el-form-item :label="item.label" :prop="item.prop">
					<el-input v-model="form[item.prop]" :placeholder="item.label" :disabled="item.prop=='id'||item.prop=='run_id'||item.prop=='flow_id'" clearable ></el-input>
				</el-form-item>
			</div>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	props: {
		fieldList: Object
	},
	components: {},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "edit",
			visible: false,
			isSaveing: false,
			titleMap: {
				edit: '编辑',
				show: '查看'
			},
			//表单数据
			form: {},
			//验证规则
			rules: {
				run_id: [
					{required: true, message: '流水标识不能为空'}
				],
				flow_id: [
					{required: true, message: '流程标识不能为空'}
				],
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode='edit'){
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.bpm.bpmForm.updateBpmFormData.post({formData:JSON.stringify(this.form)});
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form = data
		}
	}
}
</script>

<style>
</style>
